<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      let obj = {
        name: "ss",
        son: {
          name: "son",
        },
        smoke: function () {
          console.log("smoke");
        },
        eat() {
          console.log(eat);
        },
      };
      for (let key in obj) {
        console.log(typeof obj[key]);
      }
      let copyObj = JSON.parse(JSON.stringify(obj));
      //他无法实现对函数 、RegExp等特殊对象的克隆
      //会抛弃对象的constructor,所有的构造函数会指向Object
      //对象有循环引用,会报错
      copyObj.son.age = 19;
      console.log(obj, copyObj);

      function deepCopy(obj) {
        //判断是否是简单数据类型，
        if (typeof obj == "object") {
          //复杂数据类型
          var result = obj instanceof Array ? [] : {};
          for (let i in obj) {
            result[i] = typeof obj[i] == "object" ? deepCopy(obj[i]) : obj[i];
          }
        } else {
          //简单数据类型 直接 == 赋值
          var result = obj;
        }
        return result;
      }

      let obj2 = deepCopy(obj);
      console.log(obj, obj2);
    </script>
  </body>
</html>
